//
//  HBHChousePositionView.m
//  HubeiHotel
//
//  Created by 张露杰 on 18/4/10.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "HBHChousePositionView.h"

#import "HBHChouseJobValueCell.h"

@interface HBHChousePositionView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, retain) NSDictionary *data;
@property (nonatomic, retain) NSDictionary *desired_income;
@property (nonatomic, retain) NSDictionary *working_life;
@property (nonatomic, retain) NSDictionary *education;
@property (nonatomic, retain) NSDictionary *job_type;

@end

@implementation HBHChousePositionView

- (instancetype)initWithData:(NSDictionary *)data desired_income:(NSDictionary *)desired_income working_life:(NSDictionary *)working_life education:(NSDictionary *)education job_type:(NSDictionary *)job_type
{
    self = [super init];
    if (self) {
        _data = data;
        _desired_income = desired_income;
        _working_life = working_life;
        _education = education;
        _job_type = job_type;
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    self.frame = CGRectMake(0, 40*screenW_6, SCREENW, SCREENH - 64 - 40*screenW_6);
    /**
     底部遮罩
     */
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    [self addSubview:backGroundView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tap];
    
    [self addSubview:self.table];
}

-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.showsHorizontalScrollIndicator = NO;
        _table.showsVerticalScrollIndicator = NO;
        _table.separatorStyle = 0;
        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _table.backgroundColor = [UIColor clearColor];
    }
    return _table;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 75*screenW_6)];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(0, 0, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(makeShare) titleColor:TextWhite font:[UIFont systemFontOfSize:16*screenW_6] title:@"确定" image:nil backGroundImage:nil];
    [view addSubview:btn];
    btn.center = CGPointMake(view.frame.size.width/2, view.frame.size.height/2);
    btn.backgroundColor = mainColor;
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 3*screenW_6;
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 75*screenW_6;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str;
    NSArray *arr;
    NSDictionary *select;
    switch (indexPath.row) {
        case 0:
            str = @"月薪范围（不限）";
            arr = _data[@"incomeList"];
            select = _desired_income;
            break;
        case 1:
            str = @"工作经历";
            arr = _data[@"workingList"];
            select = _working_life;
            break;
        case 2:
            str = @"学历要求";
            arr = _data[@"educationList"];
            select = _education;
            break;
        case 3:
            str = @"工作性质";
            arr = _data[@"jobTypeList"];
            select = _job_type;
            break;
        default:
            break;
    }
    HBHChouseJobValueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHChouseJobValueCell"];
    if (!cell) {
        cell = [[HBHChouseJobValueCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHChouseJobValueCell"];
    }
    [cell loadCellWithData:arr title:str selectData:select];
    
    cell.blcok = ^(NSDictionary *dic){
        switch (indexPath.row) {
            case 0:
                if ([dic[@"id"] isEqualToString:_desired_income[@"id"]]) {
                    _desired_income = nil;
                }else{
                    _desired_income = dic;
                }
                break;
            case 1:
                if ([dic[@"id"] isEqualToString:_working_life[@"id"]]) {
                    _working_life = nil;
                }else{
                    _working_life = dic;
                }
                break;
            case 2:
                if ([dic[@"id"] isEqualToString:_education[@"id"]]) {
                    _education = nil;
                }else{
                    _education = dic;
                }
                break;
            case 3:
                if ([dic[@"id"] isEqualToString:_job_type[@"id"]]) {
                    _job_type = nil;
                }else{
                    _job_type = dic;
                }
                break;
            default:
                break;
        }
        [tableView reloadData];
    };
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr;
    switch (indexPath.row) {
        case 0:
            arr = _data[@"incomeList"];
            break;
        case 1:
            arr = _data[@"workingList"];
            break;
        case 2:
            arr = _data[@"educationList"];
            break;
        case 3:
            arr = _data[@"jobTypeList"];
            break;
        default:
            break;
    }
    
    CGFloat h = 0;
    if (arr.count) {
        h = (arr.count/4 + (arr.count%4?1:0))*49*screenW_6 - 13*screenW_6;
    }
    
    return 46*screenW_6 + h;
}


-(void)makeShare
{
    if (_blcok) {
        _blcok(_desired_income,_working_life,_education,_job_type);
    }
    [self dismiss];
}

-(void)dismiss
{
    if (_dismissBlcok) {
        _dismissBlcok();
    }
    [self removeFromSuperview];
}

-(void)showOnView:(UIView *)view
{
    if (!view) {
        return;
    }
    [view addSubview:self];
}

@end
